package zucc.lcj.android_finalwork.me;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import zucc.lcj.android_finalwork.R;
import zucc.lcj.android_finalwork.me.MyMessage;

public class MyHelpAdapter extends RecyclerView.Adapter <MyHelpAdapter.LinearViewHolder> {
    private Context mContext;
    private List<MyMessage> messageList;

    protected static final int CHANGE_UI = 1;
    protected static final int ERROR = 2;


    public MyHelpAdapter(List<MyMessage> messageList, Context mContext) {//构造
        this.mContext=mContext;
        this.messageList = messageList;
    }
    class LinearViewHolder extends RecyclerView.ViewHolder {
        //找到组件
        private TextView title,state,content,user,time,id;
        private ImageView photo;

        public LinearViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.myhelp_item_title);
            state = itemView.findViewById(R.id.myhelp_item_state);
            content = itemView.findViewById(R.id.myhelp_item_des);
            user = itemView.findViewById(R.id.myhelp_item_user);
            time = itemView.findViewById(R.id.myhelp_item_time);
            id = itemView.findViewById(R.id.myhelp_item_id);
            photo = itemView.findViewById(R.id.myhelp_item_img);
        }
    }
    @Override
    //返回一个ViewHolder
    //public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    public LinearViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.activity_my_helpitem,parent,false));
    }

    @Override
    //绑定ViewHolder
    //public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    public void onBindViewHolder(LinearViewHolder holder, final int position) {
        // 主线程创建消息处理器
        Handler handler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                if (msg.what == CHANGE_UI) {
                    Bitmap bitmap = (Bitmap) msg.obj;
                    holder.photo.setImageBitmap(bitmap);
                }
            }
        };
        MyMessage message = messageList.get(position);
        holder.title.setText(message.getTitle());
        holder.state.setText(message.getState());
        holder.content.setText(message.getContent());
        holder.user.setText(message.getUser());
        holder.time.setText(message.getTime());
        holder.id.setText(message.getId());
        if (message.getPhoto()!= null) {
            //子线程请求网络,Android4.0以后访问网络不能放在主线程中
            if (message.getPhoto().equals("https://bkimg.cdn.bcebos.com/pic/63d0f703918fa0ec08faacc53fde4eee3d6d54fb6deb?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UxODA=,g_7,xp_5,yp_5/format,f_auto")){
                new Thread() {
                    private HttpURLConnection conn;
                    private Bitmap bitmap;

                    public void run() {
                        // 连接服务器 get 请求 获取图片
                        try {
                            //创建URL对象
                            URL url = new URL(message.getPhoto());
                            // 根据url 发送 http的请求
                            conn = (HttpURLConnection) url.openConnection();
                            // 设置请求的方式
                            conn.setRequestMethod("GET");
                            //设置超时时间
                            conn.setConnectTimeout(5000);
                            // 得到服务器返回的响应码
                            int code = conn.getResponseCode();
                            //请求网络成功后返回码是200
                            if (code == 200) {
                                //获取输入流
                                InputStream is = conn.getInputStream();
                                //将流转换成Bitmap对象
                                bitmap = BitmapFactory.decodeStream(is);
                                //将更改主界面的消息发送给主线程
                                android.os.Message msg = new android.os.Message();
                                msg.what = CHANGE_UI;
                                msg.obj = bitmap;
                                handler.sendMessage(msg);
                            } else {
                                //返回码不等于200 请求服务器失败
                                android.os.Message msg = new android.os.Message();
                                msg.what = ERROR;
                                handler.sendMessage(msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            android.os.Message msg = new android.os.Message();
                            msg.what = ERROR;
                            handler.sendMessage(msg);
                        }
                        //关闭连接
                        conn.disconnect();
                    }
                }.start();
            }
            else {
                Bitmap bitmap = BitmapFactory.decodeFile(message.getPhoto());
                holder.photo.setImageBitmap(bitmap);
            }

        }
    }

    @Override
    //获取列表长度
    public int getItemCount() {
        return messageList.size();
    }
}